#include "threadPool.h"
int func_cd(char * target_path,stringStack_t * stack,char * username)
{
    if(target_path[0]=='/'){//说明是绝对路径
        char  check_path[1024]={0};
        sprintf(check_path,"%s%s",ROOT_PATH,username);
        strcat(check_path,target_path);
        printf("%s\n",check_path);
        if(access(check_path,F_OK)==-1)//文件不存在
        {
            printf("用户cd的目标不存在\n");
            return -1;
        }
        for(int i=0;i<stack->stackSize;i++)popStack(stack);//清空栈中元素
        char * t=strtok(check_path,"/");
        while(t)
        {
            if(strcmp(t,"..")==0&&stack->pFront!=NULL)popStack(stack);
            else if(strcmp(t,".")==0);
            else pushStack(stack,t);
            t=strtok(NULL,"/");
        }
        return 0;
    }
    else{//是相对路径
        //得到工作路径
        char work_path[100]={0};
        func_pwd(work_path,stack);
        char check_path[1024]={0};
        strcat(check_path,ROOT_PATH);
        strcat(check_path,username);
        strcat(check_path,"/");
        strcat(check_path,work_path);
        strcat(check_path,"/");
        strcat(check_path,target_path);
        printf("check path%s\n",check_path);
        if(access(check_path,F_OK)==-1)//文件不存在
        {
            printf("用户cd的目标不存在\n");
            return -1;
        }
        char * t=strtok(target_path,"/");
        while(t)
        {
            if(strcmp(t,"..")==0&&stack->pFront!=NULL)popStack(stack);
            else if(strcmp(t,".")==0);
            else pushStack(stack,t);
            t=strtok(NULL,"/");
        }
    }//相对路径处理结束
    return 0;
}

